package edu.westga.cs.recommender.model;

import java.util.LinkedList;

import edu.westga.cs.recommender.interfaces.RetrieveItemMatches;
import edu.westga.cs.recommender.model.datatypes.*;
import edu.westga.cs.recommender.data.MySQLDatabase;

public class RetrieveInfo implements RetrieveItemMatches {
	//Fields
	/**
	 * The database connection class.
	 */
	private MySQLDatabase db;
	
	//Constructor
	/**
	 * Initializes the database connection class.
	 */
	public RetrieveInfo() {
		this.db = new MySQLDatabase();
	}
	
	//Methods
	/**
	 * Gets a specified user from an input login name.
	 * @param login A string containing the login name of the user to retrieve.
	 * @return A User object for the user retrieved.
	 */
	public User retrieveUser(String login) {
		String query = "SELECT * FROM users WHERE login='" + login + "'";
		LinkedList<String> temp = db.getData(query);
		String queryB = "SELECT profile_name FROM profiles WHERE user_login='" + login + "'";
		LinkedList<String> profiles = db.getData(queryB);
		User answer = new User(temp.getFirst(), temp.getLast(), profiles);
		return answer;
	}
	
	/**
	 * Retrieves the tags attached with a user's profile.
	 * @param login A string containing the login name of the user associated with the profile. 
	 * @param profile A string containing the name of the profile. (Profile names are only
	 * unique for a given user, not in the database.)
	 * @return A LinkedList<String> containing the tags associated with this profile.
	 */
	public LinkedList<String> retrieveProfileTags(String login, String profile) {
		String query = "SELECT tags FROM profiles_tags WHERE user_login='" + login +
			"' AND profiles='" + profile + "'";
		LinkedList<String> temp = db.getData(query);
		return temp;
	}
	
	/**
	 * Retrieves the items associated with a given tag.
	 * @param tag A string containing the tag to search on.
	 * @return A LinkedList<String> containing the names of items matching the given tag.
	 */
	public LinkedList<String> retrieveMatchingItemNames(String tag) {
		String query = "SELECT item FROM items_tags WHERE tag='" + tag + "'";
		LinkedList<String> temp = db.getData(query);
		return temp;
	}
	
	/**
	 * Retrieves the item data for a given item name.
	 * @param name The name of the item to retrieve information on.
	 * @return An Item containing the information on the item.
	 */
	public Item retrieveItem(String name) {
		String query = "SELECT * FROM items WHERE name='" + name + "'";
		LinkedList<String> tempOne = db.getData(query);
		String queryTwo = "SELECT tag FROM items_tags WHERE item='" + name + "'";
		LinkedList<String> tempTwo = db.getData(queryTwo);
		Item match = new Item(tempOne.get(0), tempOne.get(1), tempOne.get(2), tempOne.get(3),
				Integer.parseInt(tempOne.get(4)), tempOne.get(5), tempTwo);
		return match;
	}
}
